import Vue from 'vue';
import ElementUI from 'element-ui';
Vue.use(ElementUI, { size: 'small' });

export const base64 = img => {
    function getBase64Image(img,width,height) {//width、height调用时传入具体像素值，控制大小 ,不传则默认图像大小
        let canvas = document.createElement("canvas");
        canvas.width = width ? width : img.width;
        canvas.height = height ? height : img.height;
        let ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
        let dataURL = canvas.toDataURL('image/jpeg');
        return dataURL;
    }
    let image = new Image();
    image.crossOrigin = '';
    image.src = img;
    let deferred=$.Deferred();
    if(img){
        image.onload =function (){
            deferred.resolve(getBase64Image(image));//将base64传给done上传处理
        };
        return deferred.promise();//问题要让onload完成后再return sessionStorage['imgTest']
    }
};

export const tool_confirm = (that,msg) => {
    return that.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    })


};

export function formatDate(date, fmt) {
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    }
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + ''
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
        }
    }
    return fmt
}

function padLeftZero(str) {
    return ('00' + str).substr(str.length)
}

/***
 * 根据时间戳获取日期时间
 * fmt格式 例：yyyy-MM-dd hh:mm:ss
 */
export function getFormatDate(time, fmt) {
    try {
        var now = new Date(time)
        return now.Format(fmt)
    } catch (e) {
        if (window.console) {
            console.log(e)
        }
    }
}
/***
 * 时间格式化
 */
Date.prototype.Format = function (fmt) {
    var o = {
        'M+': this.getMonth() + 1, //月份
        'd+': this.getDate(), //日
        'h+': this.getHours(), //小时
        'm+': this.getMinutes(), //分
        's+': this.getSeconds(), //秒
        'q+': Math.floor((this.getMonth() + 3) / 3), //季度
        S: this.getMilliseconds() //毫秒
    }
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(
            RegExp.$1,
            (this.getFullYear() + '').substr(4 - RegExp.$1.length)
        )
    for (var k in o)
        if (new RegExp('(' + k + ')').test(fmt))
            fmt = fmt.replace(
                RegExp.$1,
                RegExp.$1.length == 1 ?
                o[k] :
                ('00' + o[k]).substr(('' + o[k]).length)
            )
    return fmt
}


//base64文字
// export function base64_encode(str) {
//     //下面是64个基本的编码
//     var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
//     var out, i, len;
//     var c1, c2, c3;
//     len = str.length;
//     i = 0;
//     out = "";
//     while (i < len) {
//         c1 = str.charCodeAt(i++) & 0xff;
//         if (i == len) {
//             out += base64EncodeChars.charAt(c1 >> 2);
//             out += base64EncodeChars.charAt((c1 & 0x3) << 4);
//             out += "==";
//             break;
//         }
//         c2 = str.charCodeAt(i++);
//         if (i == len) {
//             out += base64EncodeChars.charAt(c1 >> 2);
//             out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
//             out += base64EncodeChars.charAt((c2 & 0xF) << 2);
//             out += "=";
//             break;
//         }
//         c3 = str.charCodeAt(i++);
//         out += base64EncodeChars.charAt(c1 >> 2);
//         out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
//         out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
//         out += base64EncodeChars.charAt(c3 & 0x3F);
//     }
//     return out;
// }
